package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyWeiFangHongXinHui;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeWeiFangHongXinHui;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 安心分期-潍坊宏鑫汇-混量全25
 * 链接： http://47.105.104.216:2324/index/Article/index.html?cate=15
 *
 * @author gwc
 * @create 2025-08-11 下午4:49
 */
@ApiPlan(productId = "402", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5WeiFangHongXinHui implements ApiOrgPhoneMd5ConvertHandler {

    private static final Integer CHANNEL_CODE = 49;
    private static final String CHANNEL_SECRET_KEY = "19353884445wjlwj";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        // 创建实体类
        OrgDataExchangeWeiFangHongXinHui weiFangHongXinHuiData = new OrgDataExchangeWeiFangHongXinHui();
        weiFangHongXinHuiData.setChannel_code(CHANNEL_CODE);
        weiFangHongXinHuiData.setChannel_secret_key(CHANNEL_SECRET_KEY);
        weiFangHongXinHuiData.setMd5_phone(apiDataExchangeParameter.getPhoneMd5());
        return JsonUtils.toJson(weiFangHongXinHuiData);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 创建进件实体类
        OrgApplyWeiFangHongXinHui weiFangHongXinHuiData = new OrgApplyWeiFangHongXinHui();
        // 创建拓展工具类
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 渠道编码，由负责人提供
        weiFangHongXinHuiData.setChannel_code(CHANNEL_CODE);
        // 手机号加密
        AES aes = AES.createEcb(CHANNEL_SECRET_KEY.getBytes());
        String phone = aes.encryptToBase64Str(apiApplyParameter.getPhone());
        // 客户手机号，需加密传送
        weiFangHongXinHuiData.setPhone(phone);
        // 客户姓名
        weiFangHongXinHuiData.setName(apiApplyParameter.getUserName());
        // 性别，0：保密，1：男，2：女
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .when(apiApplyParameter.getSex(), "2", 0)
                .end(1);
        weiFangHongXinHuiData.setSex(sex);
        // 年龄
        weiFangHongXinHuiData.setAge(apiFieldConverter.getAgeInt());
        // 贷款额度，单位（万）
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 3)
                .when(apiApplyParameter.getLoanAmount(), "2", 5)
                .when(apiApplyParameter.getLoanAmount(), "3", 10)
                .when(apiApplyParameter.getLoanAmount(), "4", 20)
                .end(3);
        weiFangHongXinHuiData.setQuota(money);
        // 房产，0：无房，1：本地房，2：外地房
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        weiFangHongXinHuiData.setHouse(house);
        // 车产，0：无车，1：有车
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(0);
        weiFangHongXinHuiData.setCar(vehicle);
        // 社保，0：无社保，1：有社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(0);
        weiFangHongXinHuiData.setSocial_security(socialSecurity);
        // 企业纳税，0：无企税，1：有企税
        weiFangHongXinHuiData.setPay_taxes(0);
        // 信用卡，0：无信用卡，1：有信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 1)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(0);
        weiFangHongXinHuiData.setCredit_card(creditCard);
        // 营业执照，0：无营业执照，1：有营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 1)
                .end(0);
        weiFangHongXinHuiData.setBusiness_license(bizLicenseYear);
        // 微粒贷，0：无微粒贷，1：有微粒贷
        weiFangHongXinHuiData.setMicro_loan(0);
        // 公积金，0：无公积金，1：有公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(0);
        weiFangHongXinHuiData.setAccumulation_fund(providentFund);
        // 保单，0：无保单，1：有保单
        Integer insurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        weiFangHongXinHuiData.setInsurance_policy(insurance);
        // 工资代发类型，1:现金，2：银行卡
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 2)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 1)
                .end(1);
        weiFangHongXinHuiData.setIssued_type(salaryPayMethod);
        // 信用卡总额度，单位(元)
        Integer limit = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 3000)
                .when(apiApplyParameter.getCreditLimit(), "3", 10000)
                .when(apiApplyParameter.getCreditLimit(), "4", 20000)
                .when(apiApplyParameter.getCreditLimit(), "5", 30000)
                .when(apiApplyParameter.getCreditLimit(), "6", 3000)
                .end(0);
        weiFangHongXinHuiData.setCredit_card_count_quota(limit);
        // 年收入，单位(万)
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 3)
                .when(apiApplyParameter.getMonthlySalary(), "2", 6)
                .when(apiApplyParameter.getMonthlySalary(), "3", 12)
                .when(apiApplyParameter.getMonthlySalary(), "4", 18)
                .when(apiApplyParameter.getMonthlySalary(), "5", 36)
                .end(3);
        weiFangHongXinHuiData.setAnnual_income(monthlySalary);
        // 职业身份，0：无固定职业，1：工薪族，2：生意人
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 0)
                .when(apiApplyParameter.getCareer(), "3", 2)
                .when(apiApplyParameter.getCareer(), "4", 1)
                .when(apiApplyParameter.getCareer(), "5", 2)
                .end(0);
        weiFangHongXinHuiData.setProfessional_identity(career);

        return JsonUtils.toJson(weiFangHongXinHuiData);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
